/**
 * 
 */
package fhq.hcmute.edu.vn.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import fhq.hcmute.edu.vn.language.Language;
import fhq.hcmute.edu.vn.network.NetworkCard;

/**
 * @author nglehien Nov 18, 2012 - 9:50:58 PM
 */
public class AppConfig implements Serializable {
	private static final long serialVersionUID = -1355516125165251667L;
	private PictureFrameSetting pictureFrameSetting;
	private NetworkCard networkCard;
	private int language = Language.ENGLISH;

	private static AppConfig instance = null;

	public static AppConfig getInstance() {
		if (instance == null) {
			instance = load();
		}
		return instance;
	}

	private AppConfig(PictureFrameSetting pictureFrameSetting,
			NetworkCard networkCard, int language) {
		this.pictureFrameSetting = pictureFrameSetting;
		this.networkCard = networkCard;
		this.language = language;
	}

	private AppConfig() {
		this.pictureFrameSetting = new PictureFrameSetting();
		this.networkCard = null;
		this.language = Language.ENGLISH;
	}

	public void commit() {
		try {
			File folder = new File("Application Configuration");
			if (!folder.exists()) {
				folder.mkdir();
			}
			File file = new File("Application Configuration/App.Config");
			FileOutputStream fileOutputStream = new FileOutputStream(file);
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(
					fileOutputStream);
			AppConfig appConfig = new AppConfig(pictureFrameSetting,
					networkCard, language);
			objectOutputStream.writeObject(appConfig);
			objectOutputStream.close();
		} catch (IOException e) {

		}
	}

	private static AppConfig load() {
		AppConfig appConfig = null;
		File file = new File("Application Configuration/App.Config");
		try {
			if (file.exists()) {
				FileInputStream fileInputStream = new FileInputStream(file);
				ObjectInputStream objectInputStream = new ObjectInputStream(
						fileInputStream);
				appConfig = (AppConfig) objectInputStream.readObject();
				objectInputStream.close();
			} else {
				appConfig = new AppConfig();
			}
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return appConfig;
	}

	public PictureFrameSetting getPictureFrameSetting() {
		return pictureFrameSetting;
	}

	public void setPictureFrameSetting(PictureFrameSetting pictureFrameSetting) {
		this.pictureFrameSetting = pictureFrameSetting;
	}

	public NetworkCard getNetworkCard() {
		return networkCard;
	}

	public void setNetworkCard(NetworkCard networkCard) {
		this.networkCard = networkCard;
	}

	public int getLanguage() {
		return language;
	}

	public void setLanguage(int language) {
		this.language = language;
	}

}
